<?php
include_once 'controller/default.php';
include_once 'connection/connection_database.php';
include_once 'checkForm/xulychuoi.php';


class pages {

    function Insert($page) {
        $user_id = $_SESSION["id"];
        $str = trim(khongdau($_POST['pagetitle']));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        $fillAll = "SELECT * FROM cate_board_page WHERE page_link LIKE '%{$link}%'";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        
        if(isset($page["pagehotnews"])){
            $page_hotnews = "1";
        }else{
            $page_hotnews = "0";
        }
        if(isset($page["pageshowhome"])){
            $page_showhome = "1";
        }else{
            $page_showhome = "0";
        }
        $tag = substr($page["tag_data"], 0, strlen($page["tag_data"])-1);
        $t = explode(",",$tag);
        foreach($t as $_t){
            $sqlTag = "SELECT * FROM tags WHERE tag_name = '{$_t}'";
            $resultTag = mysql_query($sqlTag);
            if(mysql_num_rows($resultTag) == 0){
                $catchuoi = trim(khongdau($_t));
                $linkTag = strtolower(preg_replace('/[^a-z0-9]+/i','-',$catchuoi)); 
                $sqlInsertTag = "INSERT INTO tags(tag_name, tag_link) VALUES('{$_t}', '{$linkTag}')";
                mysql_query($sqlInsertTag);
            }
        }
        
        $date = date("Y-m-d H:i:s");
        $sql = "INSERT INTO cate_board_page(board_id, user_id, page_title, page_summary, page_content, page_date, page_image, page_hotnews, page_showhome, page_tag, page_link, seo_title, seo_description, seo_keyword)"; 
        $sql .= "VALUES('{$page["board_name"]}', '{$user_id}', '{$page["pagetitle"]}', '{$page["pagesummary"]}', '{$page["pagecontent"]}', '{$date}', '{$page["pageimage"]}', '{$page_hotnews}', '{$page_showhome}', '{$tag}', '{$link}', '{$page["seo_title"]}', '{$page["seo_description"]}', '{$page["seo_keyword"]}')";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function InsertDrafts($page) {
        $user_id = $_SESSION["id"];
        $str = trim(khongdau($_POST['pagetitle']));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        $fillAll = "SELECT * FROM cate_board_page WHERE page_link LIKE '%{$link}%'";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        
        if(isset($page["pagehotnews"])){
            $page_hotnews = "1";
        }else{
            $page_hotnews = "0";
        }
        if(isset($page["pageshowhome"])){
            $page_showhome = "1";
        }else{
            $page_showhome = "0";
        }
        $tag = substr($page["tag_data"], 0, strlen($page["tag_data"])-1);
        $t = explode(",",$tag);
        foreach($t as $_t){
            $sqlTag = "SELECT * FROM tags WHERE tag_name = '{$_t}'";
            $resultTag = mysql_query($sqlTag);
            if(mysql_num_rows($resultTag) == 0){
                $catchuoi = khongdau($_t);
                $linkTag = strtolower(preg_replace('/[^a-z0-9]+/i','-',$catchuoi)); 
                $sqlInsertTag = "INSERT INTO tags(tag_name, tag_link) VALUES('{$_t}', '{$linkTag}')";
                mysql_query($sqlInsertTag);
            }
        }
        $date = date("Y-m-d");
        $sql = "INSERT INTO cate_board_page(board_id, user_id, page_title, page_summary, page_content, page_date, page_image, page_hotnews, page_showhome, page_tag, page_link, seo_title, seo_description, seo_keyword, page_status)"; 
        $sql .= "VALUES('{$page["board_name"]}', '{$user_id}', '{$page["pagetitle"]}', '{$page["pagesummary"]}', '{$page["pagecontent"]}', '{$date}', '{$page["pageimage"]}', '{$page_hotnews}', '{$page_showhome}', '{$tag}', '{$link}', '{$page["seo_title"]}', '{$page["seo_description"]}', '{$page["seo_keyword"]}', 2)";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function Trash($page) {
        $sql = "UPDATE cate_board_page SET page_status = 0 WHERE page_id='{$page["trash"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function Restore($page) {
        $sql = "UPDATE cate_board_page SET page_status = 1 WHERE page_id='{$page["restore"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function Delete($page) {
        $sql = "DELETE FROM cate_board_page WHERE page_id='{$page["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function Update($page) {
        $user_id = $_SESSION["id"];
        $str = trim(khongdau($_POST['pagetitle']));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        $fillAll = "SELECT * FROM cate_board_page WHERE page_link LIKE '%{$link}%' and page_id<>{$page["id"]}";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        
        if(isset($page["pagehotnews"])){
            $page_hotnews = "1";
        }else{
            $page_hotnews = "0";
        }
        if(isset($page["pageshowhome"])){
            $page_showhome = "1";
        }else{
            $page_showhome = "0";
        }
        $tag = substr($page["tag_data"], 0, strlen($page["tag_data"])-1);
        $t = explode(",",$tag);
        foreach($t as $_t){
            $sqlTag = "SELECT * FROM tags WHERE tag_name = '{$_t}'";
            $resultTag = mysql_query($sqlTag);
            if(mysql_num_rows($resultTag) == 0){
                $catchuoi = khongdau($_t);
                $linkTag = strtolower(preg_replace('/[^a-z0-9]+/i','-',$catchuoi)); 
                $sqlInsertTag = "INSERT INTO tags(tag_name, tag_link) VALUES('{$_t}', '{$linkTag}')";
                mysql_query($sqlInsertTag);
            }
        }
        $sql = "UPDATE cate_board_page SET board_id='{$page["board_name"]}', user_id='{$user_id}', page_title='{$page["pagetitle"]}', page_summary='{$page["pagesummary"]}', page_content='{$page["pagecontent"]}', page_image='{$page["pageimage"]}', page_hotnews='{$page_hotnews}', page_showhome='{$page_showhome}', page_tag='{$tag}', page_link='{$link}', seo_title='{$page["seo_title"]}', seo_description='{$page["seo_description"]}', seo_keyword='{$page["seo_keyword"]}' WHERE page_id='{$page["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function UpdateDrafts($page) {
        $user_id = $_SESSION["id"];
        $str = trim(khongdau($_POST['pagetitle']));
        $link = strtolower(preg_replace('/[^a-z0-9]+/i','-',$str)); 
        $fillAll = "SELECT * FROM cate_board_page WHERE page_link LIKE '%{$link}%' and page_id<>{$page["id"]}";
        $rs = mysql_query($fillAll);
        if(($rows = mysql_num_rows($rs)) > 0){
            $link .= "-".$rows;
        }
        
        if(isset($page["pagehotnews"])){
            $page_hotnews = "1";
        }else{
            $page_hotnews = "0";
        }
        if(isset($page["pageshowhome"])){
            $page_showhome = "1";
        }else{
            $page_showhome = "0";
        }
        $tag = substr($page["tag_data"], 0, strlen($page["tag_data"])-1);
        $sql = "UPDATE cate_board_page SET board_id='{$page["board_name"]}', user_id='{$user_id}', page_title='{$page["pagetitle"]}', page_summary='{$page["pagesummary"]}', page_content='{$page["pagecontent"]}', page_image='{$page["pageimage"]}', page_hotnews='{$page_hotnews}', page_showhome='{$page_showhome}', page_tag='{$tag}', page_link='{$link}', seo_title='{$page["seo_title"]}', seo_description='{$page["seo_description"]}', seo_keyword='{$page["seo_keyword"]}' WHERE page_id='{$page["id"]}' and page_status=2";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function FillAll() {
        $sql = "SELECT * FROM cate_board_page cb, cate_board c, category ct WHERE cb.board_id=c.board_id and c.category_id = ct.category_id and page_status = 1";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function FillAllTrash() {
        $sql = "SELECT * FROM cate_board_page cb, cate_board c, category ct, users u WHERE cb.board_id=c.board_id and c.category_id = ct.category_id and cb.user_id=u.user_id and page_status = 0";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function FillAllDrafts() {
        $sql = "SELECT * FROM cate_board_page cb, cate_board c, category ct, users u WHERE cb.board_id=c.board_id and c.category_id = ct.category_id and cb.user_id=u.user_id and page_status = 2";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function FillOne($page) {
        $sql = "SELECT * FROM cate_board_page WHERE page_id='{$page["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($result);
    }
    
    function FillOneTrash(){
        $sql = "SELECT * FROM cate_board_page WHERE page_status='0'";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_num_rows($result);
    }
    
    function FillOneDrafts(){
        $sql = "SELECT * FROM cate_board_page WHERE page_status='2'";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_num_rows($result);
    }
    
}

?>
